package org.concept.chinachesspieces.core;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

public class ChessComputer {
	public static final int RED_WIN = 1;
	public static final int RED_FAIL = 2;
	public static final int PEACE = 3;
	public static final int OVERLEVEL = 4;
	public static Logger log = LogManager.getLogger(ChessComputer.class);

	public static int computerRed(byte[][] qj, int[] his, int level,
			int maxlevel, boolean simplemode) {
		int[] actions = RedStep.stepRed(qj);
		int count = actions[0] ;//+ actions[1];
		if (count == 0)
			return RED_FAIL;
		if (level >= maxlevel)
			return OVERLEVEL;
		int peace = 0;
		int redfail = 0;
		int overlevel = 0;
		for (int i = 2; i < actions[0] + 2; i++) {
			int action = actions[i];
			int from = action / 100;
			int to = action - from * 100;
			int fromx = from / 10;
			int fromy = from - fromx * 10;
			int tox = to / 10;
			int toy = to - tox * 10;
			byte over = qj[tox][toy];
			qj[tox][toy] = qj[fromx][fromy];
			qj[fromx][fromy] = Constants.EMPTY;

			// 
			if (!simplemode
					&& computerBlack(qj, his, level + 1, level + 3, true) % 10 == RED_FAIL) {
				qj[fromx][fromy] = qj[tox][toy];
				qj[tox][toy] = over;
				redfail = action * 10 + RED_FAIL;
				continue;
			}
			int result = computerBlack(qj, his, level + 1, maxlevel, simplemode);
			qj[fromx][fromy] = qj[tox][toy];
			qj[tox][toy] = over;
			int flag = result % 10;
			if (flag == RED_WIN) {
				return action * 10 + RED_WIN;
			} else if (flag == RED_FAIL) {
				redfail = action * 10 + RED_FAIL;
			} else if (flag == PEACE) {
				peace = action * 10 + PEACE;
			} else if (flag == OVERLEVEL) {
				overlevel = action * 10 + OVERLEVEL;
			} else {
				// do something
			}
		}

//		for (int i = 114; i > 114 - actions[1]; i--) {
//			int action = actions[i];
//			int from = action / 100;
//			int to = action - from * 100;
//			int fromx = from / 10;
//			int fromy = from - fromx * 10;
//			int tox = to / 10;
//			int toy = to - tox * 10;
//			byte over = qj[tox][toy];
//			qj[tox][toy] = qj[fromx][fromy];
//			qj[fromx][fromy] = Constants.EMPTY;
//			//
//			if (!simplemode
//					&& computerBlack(qj, his, level + 1, level + 3, true) % 10 == RED_FAIL) {
//				qj[fromx][fromy] = qj[tox][toy];
//				qj[tox][toy] = over;
//				redfail = action * 10 + RED_FAIL;
//				continue;
//			}
//			int result = computerBlack(qj, his, level + 1, maxlevel, simplemode);
//			qj[fromx][fromy] = qj[tox][toy];
//			qj[tox][toy] = over;
//			int flag = result % 10;
//			if (flag == RED_WIN) {
//				return action * 10 + RED_WIN;
//			} else if (flag == RED_FAIL) {
//				redfail = action * 10 + RED_FAIL;
//			} else if (flag == PEACE) {
//				peace = action * 10 + PEACE;
//			} else if (flag == OVERLEVEL) {
//				overlevel = action * 10 + OVERLEVEL;
//			} else {
//				// do something
//			}
//		}
		if (peace != 0)
			return peace;
		else if (overlevel != 0)
			return overlevel;
		else
			return redfail;
	}

	public static int computerRed(byte[][] qj, int level) {
		int[] his = new int[level + 2];
		return computerRed(qj, his, 1, level, false);
	}

	public static int computerBlack(byte[][] qj, int level) {
		int[] his = new int[level + 2];
		return computerBlack(qj, his, 1, level, false);
	}

	public static int computerBlack(byte[][] qj, int[] his, int level,
			int maxlevel, boolean simplemode) {
		int[] actions = BlackStep.stepBlack(qj);
		int count = actions[0] + actions[1];
		if (count == 0) {
			return RED_WIN;
		}
		if (level >= maxlevel)
			return OVERLEVEL;
		int peace = 0;
		int blackfail = 0;
		int overlevel = 0;
		for (int i = 2; i < actions[0] + 2; i++) {
			int action = actions[i];
			int from = action / 100;
			int to = action - from * 100;
			int fromx = from / 10;
			int fromy = from - fromx * 10;
			int tox = to / 10;
			int toy = to - tox * 10;
			byte over = qj[tox][toy];
			qj[tox][toy] = qj[fromx][fromy];
			qj[fromx][fromy] = Constants.EMPTY;

			// 
			if (!simplemode
					&& computerRed(qj, his, level + 1, level + 3, true) % 10 == RED_WIN) {
				qj[fromx][fromy] = qj[tox][toy];
				qj[tox][toy] = over;
				blackfail = action * 10 + RED_WIN;
				continue;
			}
			int result = computerRed(qj, his, level + 1, maxlevel, simplemode);
			qj[fromx][fromy] = qj[tox][toy];
			qj[tox][toy] = over;
			int flag = result % 10;
			if (flag == RED_FAIL) {
				return action * 10 + RED_FAIL;
			} else if (flag == RED_WIN) {
				blackfail = action * 10 + RED_WIN;
			} else if (flag == PEACE) {
				peace = action * 10 + PEACE;
			} else if (flag == OVERLEVEL) {
				overlevel = action * 10 + OVERLEVEL;
			} else {
				// do something
			}
		}
		for (int i = 114; i > 114 - actions[1]; i--) {
			int action = actions[i];
			int from = action / 100;
			int to = action - from * 100;
			int fromx = from / 10;
			int fromy = from - fromx * 10;
			int tox = to / 10;
			int toy = to - tox * 10;
			byte over = qj[tox][toy];
			qj[tox][toy] = qj[fromx][fromy];
			qj[fromx][fromy] = Constants.EMPTY;
			// 
			if (!simplemode
					&& computerRed(qj, his, level + 1, level + 3, true) % 10 == RED_WIN) {
				qj[fromx][fromy] = qj[tox][toy];
				qj[tox][toy] = over;
				blackfail = action * 10 + RED_WIN;
				continue;
			}
			int result = computerRed(qj, his, level + 1, maxlevel, simplemode);
			qj[fromx][fromy] = qj[tox][toy];
			qj[tox][toy] = over;
			int flag = result % 10;
			if (flag == RED_FAIL) {
				return action * 10 + RED_FAIL;
			} else if (flag == RED_WIN) {
				blackfail = action * 10 + RED_WIN;
			} else if (flag == PEACE) {
				peace = action * 10 + PEACE;
			} else if (flag == OVERLEVEL) {
				overlevel = action * 10 + OVERLEVEL;
			} else {
				// do something
			}
		}
		if (peace != 0)
			return peace;
		else if (overlevel != 0)
			return overlevel;
		else
			return blackfail;
	}

	public static void main(String[] args) {
		byte[][] qj = {
		        // 0  1    2   3   4   5   6   7   8
				{ 00, 01, 02, 00, 15, 00, 00, 00, 00 }, // 0
				{ 00, 00, 00, 00, 00, 00, 00, 11, 06 }, // 1
				{ 00, 00, 00, 00, 00, 00, 12, 00, 00 }, // 2
				{ 00, 00, 00, 00, 00, 00, 00, 00, 00 }, // 3
				{ 00, 00, 00, 00, 00, 00, 00, 00, 00 }, // 4
				{ 00, 00, 00, 00, 00, 00, 00, 00, 00 }, // 5
				{ 00, 00, 00, 00, 00, 00, 00, 00, 00 }, // 6
				{ 00, 00, 00, 00, 17, 00, 00, 00, 00 }, // 7
				{ 00, 00, 00, 17, 00, 00, 00, 00, 00 }, // 8
				{ 16, 00, 11, 04, 05, 00, 00, 00, 00 } // 9
		};
		qj = Utils.turnRedBlack(qj);
		Test.display(qj);
		int result = ChessComputer.computerRed(qj, 10);
		System.out.println(result % 10);
		System.out.println(result / 10);
	}
}